package com.nq.controller;

import cn.hutool.core.map.MapUtil;
import com.google.common.collect.ImmutableMap;
import com.nq.common.ServerResponse;
import com.nq.dao.MyDao;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 大宗交易
 */
@RestController
@RequestMapping("/api/dz")
public class DaZhongController {

    @Resource
    SqlSessionTemplate sqlSessionTemplate;
    @Resource
    MyDao myDao;

    @RequestMapping("/list")
    public Object list(Map<String, Object> param) {
        List<Map<String, Object>> items = sqlSessionTemplate.selectList("DaZhongMapper.list", param);
        long pageNum = MapUtil.getLong(param, "pageNum", 1L);
        long pageSize = MapUtil.getLong(param, "pageSize", 10L);
        List<?> list = items.stream().skip(pageSize * (pageNum - 1)).limit(pageSize).collect(Collectors.toList());
        return ServerResponse.createBySuccess(ImmutableMap.of("total", items.size(), "list", list));
    }


    @RequestMapping("/create")
    public Object create(Map<String, Object> item) {
        MyDao.batchInsert("stock_dz", Collections.singletonList(item));
        return ServerResponse.createBySuccessMsg("fail");
    }

    @RequestMapping("/update")
    public Object update(Map<String, Object> item) {
        int flag = sqlSessionTemplate.update("DaZhongMapper.update", item);
        if (flag > 0) {
            return ServerResponse.createBySuccessMsg("success");
        }
        return ServerResponse.createByErrorMsg("fail");
    }

    //   @RequestMapping("/delete")
    //    public Object update(long id) {
    //    int flag = MyDao.deleteById("stock_dz", id);
    //       if (flag > 0) {
    //            return ServerResponse.createBySuccessMsg("操作成功");
    //       }
    //       return ServerResponse.createByErrorMsg("操作失败");
    //    }

}
